package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.*;
import com.study.selection.service.*;
import com.study.selection.utils.MD5Utils;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/s_student")
public class S_StudentController {
    // 注入Service
    @Autowired
    private CourseService courseService;

    @Autowired
    private SelectService selectService;

    @Autowired
    private StudentService studentService;
    @Autowired
    private NewsService newsService;

    //查看所有课程
    @RequestMapping("/getCourse")
    public String doList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<Course> courseList =courseService .findAll();
        PageInfo<Course> info = new PageInfo<>(courseList,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/Student/courseselection.jsp";
    }

//    @RequestMapping("/courseSelection")
//    public String getCourseSelection(Integer studentId,Integer courseId){
//        Student student=studentService.getByStudentId(studentId);
//        Course course=courseService.getByCourseId(courseId);
//        Select select=new Select();
//        select.setStudentId(student.getStudentId());
//        select.setTeacherId(course.getTeacherId());
//        select.setCourseId(course.getCourseId());
//        selectService.addSelect(select);
//        return "redirect:getCourse";
//    }

    //查看选课信息
    @RequestMapping("/getMyCourse")
    public String getMyCourseSelection(HttpSession session, Model model){
        Student student = (Student)session.getAttribute("student");
        List<Select> selectList=selectService.getByStudentId(student.getStudentId());
        model.addAttribute("selectList",selectList);
        return "/view/Student/view.jsp";
    }

    //退选
    @RequestMapping("/delSelection")
    public String delMyCourseSelection(Integer selectId){
        boolean result=selectService.deleteSelect(selectId);
        return "redirect:getMyCourse";
    }
    //获取学生成绩
    @RequestMapping("/getMyGrade")
    public String getMyCourseSelectionGrade(HttpSession session, Model model){
        Student student = (Student)session.getAttribute("student");
        List<Select> selectList=selectService.getByStudentId(student.getStudentId());
        model.addAttribute("selectList",selectList);
        return "/view/Student/grade.jsp";
    }
    //修改密码
    @RequestMapping("/updatePassword")
    public String updatePassword(String oldPwd, String newPwd1, Model model, HttpSession session){
        Student student = (Student) session.getAttribute("student");
        if (student!=null&&student.getPassword().equals(MD5Utils.getMD5(oldPwd))){
            student.setPassword(MD5Utils.getMD5(newPwd1));
            studentService.updateStudent(student);
            model.addAttribute("result","密码修改成功!");
            return "/view/Student/newPwdAgain.jsp";
        }
        else {
            model.addAttribute("result","与原密码不匹配!");
            return "/view/Student/newPwdAgain.jsp";
        }
    }
    //获取新闻
    @RequestMapping("/listAllNews")
    public String doNewsList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<News> list = newsService.findAll();
        PageInfo<News> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/Student/news.jsp";
    }


}



